package com.alibaba.cirtus.ots.application.adapter.ability.facade.serviceV2.sap;


import com.alibaba.cirtus.ots.application.adapter.ability.model.Result;
import com.alibaba.cirtus.ots.application.adapter.ability.utils.HttpTokenUtil;
import com.alibaba.cirtus.ots.application.adapter.ability.utils.HttpUtils;
import com.alibaba.citrus.ots.common.annotation.ProcessInvoker;
import com.alibaba.cz.base.tool.log.Log;
import com.alibaba.fastjson.JSON;
import com.epoch.app.otsapplicationadaptersdk.contants.OtsApplicationAdapterSdkConstants;
import com.epoch.app.otsapplicationadaptersdk.mnsapif026v2.dto.ShippingOrderDetailFeedbackRequest;
import com.epoch.app.otsapplicationadaptersdk.mnsapif026v2.service.MnSapIf026V2Service;
import com.epoch.app.otsapplicationadaptersdk.sap.MnSapCommResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Primary;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;

import java.util.Map;

/**
 *  SAP装运单明细回传
 */
@Component
@Primary
public class MnSapIf026V2ServiceImpl implements MnSapIf026V2Service {

    private final Log log = Log.getLogger(MnSapIf026V2ServiceImpl.class);

    @Autowired
    private HttpUtils httpUtils;


    /**
     * shippingOrderDetailFeedback
     */
    @Override
    @ProcessInvoker(remark = "MnSapIf026V2Service", errorCode = "OTS-02-007-00-16-999", printLog = true)
    public MnSapCommResponse shippingOrderDetailFeedback(ShippingOrderDetailFeedbackRequest mnSapIf026Request) {
        log.info("MnSapIf026V2ServiceImpl_shippingOrderDetailFeedback: param:{}", JSON.toJSONString(mnSapIf026Request));
        Map<String, String> header = HttpTokenUtil.generateHttpHeader();
        String urlPrefix = OtsApplicationAdapterSdkConstants.urlPrefix();
        String url = urlPrefix + "api/mn_bc_link/shippingOrderDetailInfoPostback";
        log.info("MnSapIf026V2ServiceImpl shippingOrderDetailFeedback, http header:{}, url:{}", header, url);
        MnSapCommResponse response = new MnSapCommResponse();
        mnSapIf026Request.getMessageHeader();
//        response.setInterfacePath(mnSapIf026Request.getMessageHeader().);
//        response.setMessageId(mnSapIf026Request.getMessageHeader().getMessageId());
        try {

            log.info("MnSapIf026V2ServiceImpl shippingOrderDetailFeedback call bc link interface,url:{}, header:{}, param:{}", url, header, JSON.toJSONString(mnSapIf026Request));
            ResponseEntity<Result> post = httpUtils.post(url, header, mnSapIf026Request, Result.class);
            Result result = post.getBody();
            log.info("MnSapIf026V2ServiceImpl shippingOrderDetailFeedback, result:{}", JSON.toJSONString(result));
        } catch (Exception e) {
            log.error("MnSapIf026V2ServiceImpl shippingOrderDetailFeedback, error:{}", e);
        }
        return response;
    }
}
